﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsideDL.MISService;
using InsideDL.MISControl;
using InsideDL.MISData;


namespace InsideDL
{
    public partial class frmSaveCenter : MISControl.frmMatrix 
    {
        protected MISService.VerifyMatrix verifyObj = null;
        protected object[] ftpPara = null;
        protected EXLAPP exlAPP = null;
        protected string saveFilePath
        {
            get
            {
                return ucALMFilePath.Txt;
            }
        }

        public frmSaveCenter()
        {
            InitializeComponent();
            this.controlList = new List<ucSelFileOrPath>
            {
                ucALMFilePath,ucSaveCen
            };
            ucExecute1.ExecuteClick += new EventHandler(ucClickFacade);
            ucExecute_Exit.ExecuteClick += new EventHandler(ucClickFacade);
        }

        protected void executeFromFTP()
        {
            Utility.LoadConfigValue("FTPCollection");
            ftp = Utility.GetConfigValue("ftp");
            id = Utility.GetConfigValue("ftpid");
            pw = Utility.GetConfigValue("ftppw");
           
            //if (!checkFileInFTPSite())
            //{ return; }

            if (!checkFileExist(Utility.GetCurrentYM()
                , Utility.GetFTPFileList(ftp, id, pw)))
            { return; }

            Utility.DownloadFile(ftp, id, pw, downList, saveFilePath);
        }

        //執行之前月份的，而不是從FTP download
        protected void executeFromBefore()
        { 
            //取得downlist
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(ucALMFilePath.Txt);
            System.IO.FileInfo[] fis = di.GetFiles("*.csv");
            System.Collections.ArrayList al = new System.Collections.ArrayList();
            foreach (System.IO.FileInfo fi in fis)
            {
                al.Add(fi.Name);
            }

            if (!checkFileExist(ucYYYYMM.Txt
               , (string[])al.ToArray(typeof(string)) ))
            { return; }
        }

        protected override void execute()
        {
            if (base.Vaildation().Equals(false))
            { return; }
            /*
            Utility.LoadConfigValue("FTPCollection");
            ftp = Utility.GetConfigValue("ftp");
            id = Utility.GetConfigValue("ftpid");
            pw = Utility.GetConfigValue("ftppw");
            if (base.Vaildation().Equals(false))
            { return; }
            if (!checkFileInFTPSite())
            { return; }
            Utility.DownloadFile(ftp, id, pw, downList, saveFilePath);
            */
            string flName,twym;
            flName = string.Empty;
            twym = Utility.GetTWYM();
            if (ucYYYYMM.Visible.Equals(false))
            {
                executeFromFTP();
                flName = string.Format("信用卡用原版-{0}存央等"
                , twym);
            }
            else
            {
                twym = Utility.GetTWYM(ucYYYYMM.Txt);
                executeFromBefore();
                flName = string.Format("信用卡用原版-{0}存央等"
                , twym);
            }

            DataInside di = new DataInside();
            Program.CreateFile(saveFilePath + "\\schema.ini",
               Utility.GetARTMACDSchema(downList, di.GetCSVFields(downList,
               saveFilePath)));
            DataSaveCen dataSav = new DataSaveCen();
            object data = dataSav.GetDataRS(downList, saveFilePath
                , Utility.GetThisLastMonth(downList[0])
                , Utility.GetCurrentDay(downList[0]));
            //I資料
            //信用卡用原版-9912存央等
            //flName = string.Format("信用卡用原版-{0}存央等"
            //    ,Utility.GetTWYM());

            createNewFile(flName, System.IO.Path.GetDirectoryName(ucSaveCen.Txt)
                , ucSaveCen.Txt);
            exlAPP = new EXLAPP();

            exlAPP.OpenFile(newFile);
            exlAPP.CopyFromRecordset((ADODB.RecordsetClass)data, "A2:G60000"
                , "A2:G60000", "I資料");

            string[] refreshTables = new[] { "資料" };
            foreach (string rf in refreshTables)
            {
                exlAPP.RefreshPivotTables(rf);
            }
            string day = Utility.GetCurrentDay(downList[0]);
            exlAPP.SetRangeValue("央行等量率", "a1", day);
            exlAPP.SetRangeValue("央行等量率", "b2"
                , string.Format("'{0}", twym));
            exlAPP.SetRangeValue("I資料", "I1"
                , string.Format("'{0}", twym));
            exlAPP.SaveCloseXLS();
            //releaseObject(exlAPP.exlAPP);
            showMessage(this.Text, "完成!");
        }



        //protected void createNewFile()
        //{
        //    //也許可以用Path.Combine
        //    newFile = string.Format(@"{1}\{0}債票券.xls"
        //        , Utility.GetTWYM()
        //        , System.IO.Path.GetDirectoryName(ucTicketFile.Txt)
        //        );
        //    File.Copy(ucTicketFile.Txt, newFile, true);
        //    //lastExeceFile = target;
        //}



        //protected bool checkFileInFTPSite()
        //{
        //    verifyObj = new InsideDL.MISService.VerifyTickey();
        //    ftpPara = new object[]{ Utility.GetCurrentYM(),
        //    Utility.GetFTPFileList(ftp, id, pw) };
        //    bool ok = verifyObj.CheckFileInFtp(ftpPara);
        //    if (ok == true)
        //    {
        //        downList = verifyObj.GetDownList();
        //    }
        //    return ok;
        //}

        protected bool checkFileExist(string p_ym, string[] p_list)
        {
            verifyObj = new InsideDL.MISService.VerifyTickey();
            ftpPara = new object[]{ p_ym,p_list};
            bool ok = verifyObj.CheckFileInFtp(ftpPara);
            if (ok == true)
            {
                downList = verifyObj.GetDownList();
            }
            return ok;
        }

        private void frmSaveCenter_DoubleClick(object sender, EventArgs e)
        {
            //dbouble click 時就出現可以輸入之前月份的控制項
            ucYYYYMM.Visible = !ucYYYYMM.Visible;
        }

    }
}
